//全局变量
var loginUserName = "";
var loginUserId = "";
var loginUserType = "";
var loginProvince = "";
var loginCity = "";
var loginArea = "";
var addOrUpdateUserName = "";
var _imgesByUrl = 'http://img.boyitongcheng.com/';
var bizDealCount = 0;
var bizYdCount = 0;
var money = 0;
$(function(){
    //首次加载主页面
    $(".mainDiv").load("systemIndexHtml",'',function(){
        $.getScript("../../by-webhtml/assets/js/system/system_index.js");
    });
    toOrderPage();

    //判断是否登录
    var mainHtmlTopRightFunction = function(){
        $.ajax({
            url:"loginWeb/isLogin.json",
            type:'GET',
            dataType:"json",
            success:function(result){
                if(result.isLogin == 'OK'){
                    var tem = "<a class='ico5' href=\"#\" onclick=\"unLoginFunction();\">注销</a>" +
                        "<em></em>" +
                        "<a class='ico4' href=\"#\">"+result.userName+"</a>" +
                        "<em></em>" +
                        "<a class='ico3' href=\"/\">博艺同城首页</a>" +
                        "<em></em>" +
                        "<a class='ico2' href=\"#\" onclick=\"goMyTrolley()\">我的购物车</a>" +
                        "<em></em>" +
                        "<a class='ico1' href=\"tel:028-86251402\">028-86251402</a>";

                    $("#systemMainTopL").empty();
                    $("#systemMainTopL").append(tem);
                    loginUserName = result.userName;
                    loginUserId = result.userId;
                    loginUserType = result.userType;
                    loginProvince = result.province;
                    loginCity = result.city;
                    loginArea = result.area;
                    bizDealCount = result.bizDealCount;
                    bizYdCount = result.bizYdCount;
                    money = result.money;

                    //获取左侧导航
                    $.ajax({
                        type: 'post',
                        url: 'usersWeb/findAllRescByRoleIdFromUserId.json',
                        dataType: 'json',
                        data: {roleId:0},
                        success: function (result) {
                            var tem = "";

                            $.each( result.rescMenuList, function(index, content){
                                if(content.name == '系统首页'){
                                    tem += "<li class='cur'>";
                                }else {
                                    tem += "<li>";
                                }
                                tem += "<h2><a href=\"#\" url_val='"+content.showUrl+"' lineNum='"+content.showMenu+"'><span><img src=\"../../by-webhtml/assets/image/system/"+content.resString+"\" />"+content.name+"</span></a></h2>";
                                if(content.rescList != null){
                                    tem += "<div class='leftDiv'>";
                                    var rescList = content.rescList;
                                    $.each(rescList, function(index, content){
                                        tem += "<h3>";
                                        tem += "<a class='onDiv' href=\"#\" url_val='"+content.showUrl+"' lineNum='"+content.showMenu+"'>"+content.name+"</a>";
                                        if(content.rescList != null){
                                            var rescList1 = content.rescList;
                                            tem += "<div class='h3Div'>";
                                            $.each(rescList1, function(index, content){
                                                if(content.name == '编辑资料'){
                                                    tem += "<a href=\"#\" onclick=\"updateUserSystem();\">"+content.name+"</a>";
                                                }else {
                                                    tem += "<a href=\"#\" url_val='"+content.showUrl+"' lineNum='"+content.showMenu+"'>"+content.name+"</a>";
                                                }
                                            });
                                            tem += "</div>";
                                        }
                                        tem += "</h3>";
                                    });
                                    tem += "</div>";
                                }
                                tem += "</li>";
                            });

                            $("#systemMenu").empty();
                            $("#systemMenu").append(tem);

                            //左侧导航
                            $(".leftNav").height($(window).height() - 60 + "px");
                            $(".chanPass").height($(window).height() - 180 + "px");




                            $(".leftNav ul li,.h3Div a").click(function(){
                                $(this).addClass("cur").siblings().removeClass("cur");
                            });
                            $(".leftNav ul li h2").click(function(){
                                $(this).next(".leftDiv").slideToggle().parent(".leftNav ul li").siblings().children(".leftDiv").slideUp();
                            });
                            $(".leftDiv h3 .onDiv").click(function(){
                                $(this).parent(".leftDiv h3").addClass("on").siblings().removeClass("on");
                                $(this).next(".h3Div").slideToggle();
                            });

                            //对后台主界面的菜单添加点击事件
                            $('.leftNav a').click(function () {
                                navPills($(this).attr("url_val"),$(this).attr("lineNum"));
                            });
                        },
                        error: function() {
                            alert("数据请求失败");
                        }
                    });
                }else {
                    window.location.href="/";
                }
            }
        });
    }();
});

function getUrlParam(name){
    var LocString = String(window.document.location.href);
    var rs = new RegExp("(^|)" + name + "=([^&]*)(&|$)", "gi").exec(LocString);
    if(rs == null){
        return "";
    }
    return decodeURI(rs[2]);
}

function toOrderPage(){
    if(getUrlParam("flag").indexOf('order') != -1){
        $(".mainDiv").load("systemMineOrderHtml",'',function(){
            $.getScript("../../by-webhtml/assets/js/system/system_mine_order.js");
        });
    }
}

function goMyTrolley() {
    $.ajax({
        type: 'get',
        url: '/loginWeb/isLogin.json',
        dataType: 'json',
        data: {},
        success: function (ret) {
            if (ret.isLogin == 'OK') {
                window.location.href = "/?flag='trolley'";
            } else {
                navPills('loginHtml');
            }
        },
        error: function(e) {
            console.log('error')
        }
    });
}

//菜单点击事件main切换页面(路由\动态加载js)
/**
 *
 * @param url
 * @param lineNum  默认为1  下划线的数量
 */
var navPills = function(url,lineNum){
    if(url == undefined) return;
    if(url){
        var jsName = "";
        if(url.indexOf("system") != -1){
            var rePath = url.split("system")[1];
            if(!lineNum){
                jsName = "system_" + rePath.substring(0,1).toLowerCase() + rePath.substring(1);
            }else{
                var num = 0;
                jsName = "system";
                for(var i = 0;i < rePath.length;i++){
                    var ch = rePath.charAt(i);
                    if(ch == ch.toUpperCase() && num < lineNum){
                        ch = "_" + ch.toLowerCase();
                        num++;
                    }
                    jsName += ch;
                }
            }
        }
        jsName = jsName.split("Html")[0];

        if(jsName == 'system_adverList'){
            if(loginUserType == 1 || loginUserType == 5){
            }else {
                alert("广告管理只有管理员和代理商有权限！");
                return;
            }
        }

        var jsPath = "../../by-webhtml/assets/js/system/" + jsName + ".js";
        $(".mainDiv").load(url,'',function(){
            $.getScript(jsPath);
        });
    }
};

//注销
var unLoginFunction = function(){
    $.ajax({
        url:"loginWeb/unLogin.json",
        type:'GET',
        dataType:"json",
        success:function(result){
            window.location.href="/";
        }
    });
}

//点击编辑用户
var updateUserSystem = function(){
    addOrUpdateUserName = loginUserName;
    navPills('systemEditDataHtml',2);
}

//时间转换
function renderDateStr(createTime) {
    if(createTime != null){
        return createTime.year +"-"+createTime.monthOfYear +"-"+createTime.dayOfMonth+" "+createTime.hourOfDay+":"+createTime.minuteOfHour+":"+createTime.secondOfMinute;
    }else {
        return "";
    }
}